package it.mauriziulu.android.ehttpexecutors.entity;

import java.io.Serializable;
import java.util.Date;

import org.apache.http.Header;
import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;

public class CacheMetaEntry implements Serializable {
	private static final long serialVersionUID = -8530124861177588208L;

	private static final String CONTENT_TYPE = "content-type";

	private static final String DATE = "date";

	private static final String ETAG = "etag";

	private static final String CACHE_CONTROL = "cache-control";

	private String contentType;
	private String eTag;
	private String cacheControl;
	private Date date;

	public CacheMetaEntry(Header[] responseHeaders) {
		structureData(responseHeaders);
	}

	private void structureData(Header[] responseHeaders) {
		if (responseHeaders != null) {
			for (Header header : responseHeaders) {
				if (ETAG.equalsIgnoreCase(header.getName())) {
					eTag = header.getValue();
				} else if (DATE.equalsIgnoreCase(header.getName())) {
					try {
						date = DateUtils.parseDate(header.getValue());
					} catch (DateParseException e) {
						e.printStackTrace();
					}
				} else if (CONTENT_TYPE.equalsIgnoreCase(header.getName())) {
					contentType = header.getValue();
				} else if (CACHE_CONTROL.equalsIgnoreCase(header.getName())) {
					cacheControl = header.getValue();
				}
			}
		}
	}

	public boolean canCache(){
		if (cacheControl!=null && cacheControl.contains("no-cache")){
			return false;
		}
		return true;
	}
	
	public String getETag() {
		return eTag;
	}

	public String getContentType() {
		return contentType;
	}

	public Date getDate() {
		return date;
	}

	public String getCacheControl() {
		return cacheControl;
	}
}
